package com.sff.site.abstract {
	
	import com.sff.core.sff_internal;
	import com.sff.site.core.INode;
	import com.sff.site.core.IPath;
	import com.sff.site.node.NodeDescriptor;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLVariables;

	public class AbstractTree extends EventDispatcher implements INode {
		
		protected var _rootNode : INode;
		
		public function get root() : INode {
			return _rootNode;
		}

		public function AbstractTree( rootNode : AbstractNode ) {
			_rootNode = rootNode;
		}

		public function addChild(node : INode) : INode {
			if( node.getId( ) == "null" ) throw new Error( "aiaiai" );
			
			return _rootNode.addChild(node);
		}

		public function getId() : String {
			return _rootNode.getId( );
		}

		public function getChilds() : Array {
			return _rootNode.getChilds( );
		}
		
		public function isActive() : Boolean {
			return _rootNode.isActive();
		}

		public function getChild(id : String) : INode {
			return _rootNode.getChild( id );
		}

		public function hasChild(id : String) : Boolean {
			return _rootNode.hasChild( id );
		}

		public function path( getDefaults : Boolean = false ) : IPath {
			return _rootNode.path( getDefaults );
		}

		public function parent() : INode {
			return _rootNode.parent( );
		}
		
		public function getCurrentChild() : INode {
			return _rootNode.getCurrentChild( );
		}
		
		public function getDefaultNode() : INode {
			return _rootNode.getDefaultNode();
		}
		
		public function getDefaultId() : String {
			return _rootNode.getDefaultId();
			
		}
		
		public function activate( params : URLVariables = null ) : void {
			throw new Error( "activate() can't be called on AbstractTree" );
		}
		
		public function getParams() : URLVariables {
			return _rootNode.getParams();
		}

		public function setParams(params : URLVariables) : void {
			_rootNode.setParams( params );
		}

		
		sff_internal function buildTreeNode( descriptor : NodeDescriptor, target : INode ) : void {
			
			var node : INode;
		
			for each (var subDesc : NodeDescriptor in descriptor.getChilds()) {
				node = newNode( subDesc , target );
				node = target.addChild( node );
				
				sff_internal::buildTreeNode(subDesc, node );
			}
		}

		
		protected function newNode( descriptor : NodeDescriptor, parent : INode ) : INode {
			return null;
			parent;
			descriptor;
		}

		public function stringify(indent : int = 0) : String {
			return "Tree : " + _rootNode.stringify( indent );
		}
		
		
		override public function dispatchEvent(event : Event) : Boolean {
			return _rootNode.dispatchEvent( event );
		}

		override public function hasEventListener(type : String) : Boolean {
			return _rootNode.hasEventListener( type );
		}

		override public function willTrigger(type : String) : Boolean {
			return _rootNode.willTrigger( type );
		}

		override public function removeEventListener(type : String, listener : Function, useCapture : Boolean = false) : void {
			_rootNode.removeEventListener( type , proxyListener , useCapture );
			super.removeEventListener( type , listener , useCapture );
		}

		override public function addEventListener(type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void {
			_rootNode.addEventListener( type , proxyListener , useCapture , priority , useWeakReference );
			super.addEventListener( type , listener , useCapture , priority , useWeakReference );
		}

		private function proxyListener( e : Event ) : void {
			super.dispatchEvent( e );
		}		
	}
}
